/**
  * 递归压缩H5图片
  * @param url 图片路径
  * @param isReturnBase64 是否返回base64
  * @param callback 回调函数
  * @return
  */
export function recursionCompressH5(url, isReturnBase64, callback) {
  uni.getImageInfo({
    src: url,
    success(res) {
      let canvasWidth = res.width; //图片原始长宽
      let canvasHeight = res.height;
      let img = new Image();
      img.src = res.path;
      let canvas = document.createElement("canvas");
      let ctx = canvas.getContext("2d");
      canvas.width = canvasWidth / 2;
      canvas.height = canvasHeight / 2;
      ctx.drawImage(img, 0, 0, canvasWidth / 2, canvasHeight / 2);
      canvas.toBlob(function (fileSrc) {
        let imgSrc = window.URL.createObjectURL(fileSrc);
        uni.getFileInfo({
          filePath: imgSrc,
          success: (resFileInfo) => {
            if (resFileInfo.size > 1024 * 1024) {
              //压缩后大于1M就继续压缩
              recursionCompressH5(imgSrc, isReturnBase64, callback);
              return;
            } else {
              //小于就转化base64
              let resultBase64 = canvas.toDataURL("image/png")
              callback && callback(!isReturnBase64 ? resultBase64: imgSrc)
            }
          },
        });
      });
    }
  });
}